package com.xyd.Filter;

import com.xyd.Entity.ManageUser;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebFilter("/html/manage/*")
public class manageLoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String path =request.getServletPath();
        System.out.println("["+path+"]\t:管理员用户登录过滤器:");
        HttpSession session =request.getSession();
        ManageUser manageUser = (ManageUser) session.getAttribute("manageUser");
        if(manageUser!=null){
            filterChain.doFilter(servletRequest, servletResponse);
        }else{
            response.sendRedirect("/shopping/login.jsp");
        }
    }

    @Override
    public void destroy() {

    }
}
